package com.zzgc.modules.controller;

import com.zzgc.common.entity.SchoolGrade;
import com.zzgc.common.entity.Student;
import com.zzgc.common.response.HttpStatus;
import com.zzgc.common.response.Result;
import com.zzgc.common.utils.LangUtil;
import com.zzgc.modules.param.Sort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 年级管理
 * @Author:
 * @CreateTime: 2022-11-24  11:24
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("grade")
@Api(tags = "年级管理")
public class GradeController extends BaseController{

    @GetMapping("getList")
    @ApiOperation(value = "查询学校年级", notes = "列表展示, 查询学校年级")
    public Result getGrades(SchoolGrade schoolGrade) {
        // 添加学校
        schoolGrade.setSSchoolId(Long.valueOf(getSchoolId()));
        // 排序
        schoolGrade.setSorts(Arrays.asList(new Sort("s_sort")));
        List<SchoolGrade> schoolGrades = schoolGradeService.queryByPage(schoolGrade);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", schoolGrades.size() == 0 ? 0 : schoolGrades.get(0).getTotal());
        for (SchoolGrade grade : schoolGrades) {
            Student student = new Student();
            student.setSGradeId(grade.getId());
            long count = studentService.count(student);
            grade.setTotal(count);
        }
        map.put("data", schoolGrades);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(map)
                .build();
    }

    @GetMapping("getById/{gradeId}")
    @ApiOperation(value = "根据ID查询年级", notes = "查询年级")
    public Result getGrade(@PathVariable Long gradeId){
        SchoolGrade schoolGrade = schoolGradeService.queryById(gradeId);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(schoolGrade)
                .build();
    }

    @PostMapping("add")
    @ApiOperation(value = "添加年级", notes = "添加年级")
    public Result addGrade(@RequestBody SchoolGrade schoolGrade){
        schoolGrade.setSSchoolId(Long.valueOf(super.getSchoolId()));
        schoolGrade.setSCreateBy(super.getUserName());
        schoolGrade.setSCreateDate(new Date());

        schoolGrade.setSYear(getsYear() + "");
        // 添加排序
        if (schoolGrade.getSName() != null){
            // 三年级 去除班字得到中文数字
            String s = schoolGrade.getSName();
            if (s.contains("高")) {
                s = s.replaceAll("高", "");
                schoolGrade.setSSort(LangUtil.hanZiToNum(s) + 20);
            }else {
                s = s.replaceAll("年", "");
                s = s.replaceAll("级", "");
                if (!LangUtil.isNumeric(s)) {
                    schoolGrade.setSSort(LangUtil.hanZiToNum(s));
                }else {
                    schoolGrade.setSSort(Integer.valueOf(s));
                }
            }
        }

        SchoolGrade insert = schoolGradeService.insert(schoolGrade);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(insert)
                .build();
    }

//    @PutMapping("edit")
//    @ApiOperation(value = "编辑年级", notes = "编辑年级")
    public Result editGrade(@RequestBody SchoolGrade schoolGrade){
        schoolGrade.setSSchoolId(Long.valueOf(super.getSchoolId()));
        schoolGrade.setSUpdateBy(super.getUserName());
        schoolGrade.setSUpdateDate(new Date());
        SchoolGrade update = schoolGradeService.update(schoolGrade);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(update)
                .build();
    }

    @DeleteMapping("del/{gradeId}")
    @ApiOperation(value = "删除年级", notes = "删除年级")
    public Result deleteById(@PathVariable Long gradeId) {
        boolean b = schoolGradeService.deleteById(gradeId, super.getStudentTable());
        if (!b) {
            return Result.builder().code(500).msg("删除年级失败!").build();
        }
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg("删除年级成功!")
                .build();
    }

}
